package com.wjc.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.util.Log;

public class Dispatcher
{
   
    public Dispatcher(){}
    
    /**
     * 
     * @param host
     * @param port
     */
   
    
    /**
     * 
     * @param request - void
     * @author: 
     */
    public static void dispatch(HttpRequest request,Socket target)
    {
      //  SocksSocket socket = null;
        try
        {
            
           target.setKeepAlive(true);
            
            OutputStream socketOutputStream = target.getOutputStream();
            InputStream socketInputStream   = target.getInputStream();
            
            request(request, socketOutputStream);
            
            HttpResponse httpResponse = HttpResponseFactory.read(socketInputStream, socketOutputStream);    
            resonse(httpResponse, request.getOutputStream());
            
        }
        catch(IOException e)
        {
        	e.printStackTrace();
            System.out.println("Warning: " + e.getMessage());
        }
     
    }
    
    /**
     * 
     * @param request
     * @throws IOException
     * @author: 
     */
   public static void request(HttpRequest request, OutputStream outputStream) throws IOException
    {
        /* 对请求头进行修改 */
        //updateHttpRequest(request,request.getRemoteHost(),request.getRemotePort());
        
        String headers = request.toString();
        
        Log.d("请求"+Thread.currentThread().getId(),"------------------ 修改后的请求头 ------------------\r\n" + headers);
        
        /* 写请求头 */
        outputStream.write(headers.getBytes());
       
        /* 将输入数据传输到服务器 */
        ContentPump.pumpContentStream(request.getInputStream(), outputStream, request.getContentLength());
       
        
    }
    
    /**
     * 
     * @param response
     * @throws IOException - void
     * @author: 
     */
    public static void resonse(HttpResponse response, OutputStream outputStream) throws IOException
    {
        /* 修改响应头 */
      //  updateHttpResponse(response);
        
        String headers = response.toString();
        
        Log.d("响应"+Thread.currentThread().getId(), "------------------ 修改后的响应头 -----------------\r\n" + headers);
        
        //outputStream = new PoolOutputStream(new OutputStream[]{outputStream, System.out});
        
        /* 写响应头 */
        outputStream.write(headers.getBytes());
        
        /* 是否使用Chunk */
        if(response.getHeader("Transfer-Encoding") != null)
        {
            InputStream inputStream = response.getInputStream();
            
            ChunkedPump.pumpChunkedStream(inputStream, outputStream);
        }
        else
        {
            InputStream inputStream = response.getInputStream();
            
            ContentPump.pumpContentStream(inputStream, outputStream, response.getContentLength());
        }
        outputStream.flush();
       
    }
    

    
    /**
     * 
     * @param request - void
     * @author: 
     */
    private static void updateHttpRequest(HttpRequest request,String host,int port)
    {
     
            request.setHeader("Connection", "Close");
   
        
  
    }
    
    /**
     * 
     * @param response - void
     * @author: 
     */
    private static void updateHttpResponse(HttpResponse response)
    {
        String header = response.getHeader("Connection");
        
        if(header != null)
        {
            Log.d("connection",response.getHeader("Connection"));
            response.setHeader("Connection", "Close");
           // if()
        }
    }
 

 
 
}
